//
//  WCLeftViewController.m
//  WeboCon
//
//  Created by black sun on 12-3-4.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WCLeftViewController.h"
#import "WCUserContext.h"


@implementation WCLeftViewController
@synthesize functionControllers;
@synthesize uids;
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        CALayer *layer = [CALayer layer];
        layer.backgroundColor =  LeftBackgroundColor;
        [self.view setWantsLayer:YES]; 
        [self.view setLayer:layer];
        
        self.functionControllers = [NSMutableArray array];
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        self.uids = [NSMutableArray arrayWithArray:[userDefault arrayForKey:UserUidListKey]];

        
        for (int i=0;i<self.uids.count;i++) {
            NSNumber *uid = [self.uids objectAtIndex:i];
            WCWeiboFunctionController *controller = [[WCWeiboFunctionController alloc] initWithUid:uid];
            controller.delegate = self;
            [self.functionControllers addObject:controller];
        }
        
        [self.tableView reloadData];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addUser:) name:NewUserLoginSuccessNotification object:nil];
        
    }
    
    return self;
}


-(void)wantToChangeUser:(NSNumber *)uid{
    selectedIndex = [self.uids indexOfObject:uid];
    [self.tableView reloadData];
}

-(void)addUser:(NSNotification *)notification{
    NSNumber *uid = [notification.userInfo objectForKey:OAuthUserIdKey];
    if (self.uids==nil) {
        self.uids = [NSMutableArray array];
    }
    [self.uids addObject:uid];
    [WCUserDefaultHelper saveUserDefaultWithObject:self.uids forKey:UserUidListKey];
    //[self addFunctionView:uid selected:YES];
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObject:self];
}
#pragma mark NSTableView DataSource and Delegate

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
    return self.functionControllers.count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    NSViewController *viewController = [self.functionControllers objectAtIndex:row];
    return viewController.view;
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{
    return row==selectedIndex?250:50;
}
@end
